package com.example.idachuappone.index.item;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Dialog;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

import com.example.idachuappone.BaseActivity;
import com.example.idachuappone.R;
import com.example.idachuappone.index.adapter.CommentAdapter;
import com.example.idachuappone.index.entity.Comment;
import com.example.idachuappone.ui.OnRefreshListener;
import com.example.idachuappone.ui.RefreshListView;
import com.example.idachuappone.utils.BitmapUtilHelper;
import com.example.idachuappone.utils.CheckUtil;
import com.example.idachuappone.utils.ComUtil;
import com.example.idachuappone.utils.Constant;
import com.example.idachuappone.utils.MainToast;
import com.example.idachuappone.utils.NetUtil;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;

public class CommentListActivity extends BaseActivity implements OnClickListener {
	private Button btn_back;
	private CommentAdapter commentAdapter;
	private Dialog dialog;
	private String id;
	private List<Comment> list;
	private RefreshListView lv_comment;
	private int page;
	private SwipeRefreshLayout swipeRefreshLayout;
	private String title;

	public CommentListActivity() {
		this.page = 1;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_comment_list);
		this.dialog = ComUtil.createCusProgressSmallBlackBgDialog(this);
		this.title = getIntent().getExtras().getString("title");
		this.id = getIntent().getExtras().getString("id");
		initView();
		if (CheckUtil.checkNet(this)) {
			this.dialog.show();
			initData();
			return;
		}
		MainToast.show(this, getResources().getString(R.string.txt_network), 0);
	}

	private void initData() {
		NetUtil.get(this, new StringBuilder(Constant.COMMENT).append(this.id).append("?page=").append(this.page)
				.toString(), 100, new RequestCallBack<String>() {
			@Override
			public void onSuccess(ResponseInfo<String> responseInfo) {
				CommentListActivity.this.dialog.dismiss();
				try {
					JSONObject jsonObject = new JSONObject(responseInfo.result);
					if (jsonObject.getInt("code") == 10000) {
						JSONArray jsonArray = jsonObject.optJSONObject("data").getJSONArray("results");
						CommentListActivity.this.list = new ArrayList();
						for (int i = 0; i < jsonArray.length(); i++) {
							try {
								JSONObject jsonObject2 = jsonArray.getJSONObject(i);
								CommentListActivity.this.list.add(new Comment().parseJson(jsonObject2));
							} catch (JSONException e) {
								e.printStackTrace();
							}
						}
						CommentListActivity.this.lv_comment.hideFooterView(true);
						CommentListActivity.this.swipeRefreshLayout.setRefreshing(false);
						CommentListActivity.this.initAdapter();
						return;
					}
					MainToast.show(CommentListActivity.this, jsonObject.getString("error"), 0);
				} catch (JSONException e2) {
					e2.printStackTrace();
				}
			}

			@Override
			public void onFailure(HttpException arg0, String arg1) {
				CommentListActivity.this.lv_comment.hideFooterView(true);
				CommentListActivity.this.swipeRefreshLayout.setRefreshing(false);
				CommentListActivity.this.dialog.dismiss();
				MainToast.show(CommentListActivity.this,
						CommentListActivity.this.getResources().getString(R.string.netFail), 0);
			}
		});
	}

	private void initView() {
		this.btn_back = (Button) findViewById(R.id.btn_back);
		this.btn_back.setOnClickListener(this);
		this.swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipeRefreshLayout);
		this.swipeRefreshLayout.setColorSchemeResources(R.color.color_two_main);
		this.lv_comment = (RefreshListView) findViewById(R.id.lv_comment);
		this.commentAdapter = new CommentAdapter(this, BitmapUtilHelper.createBitmapUtil(this));
		this.commentAdapter.setTitle(this.title);
		this.lv_comment.setAdapter(this.commentAdapter);
		this.lv_comment.setOnRefreshListener(new OnRefreshListener() {
			@Override
			public void onLoadMoring() {
				CommentListActivity commentListActivity = CommentListActivity.this;
				commentListActivity.page = commentListActivity.page + 1;
				CommentListActivity.this.addData();
			}
		});
		this.swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
			@Override
			public void onRefresh() {
				CommentListActivity.this.swipeRefreshLayout.setRefreshing(false);
				CommentListActivity.this.page = 1;
				CommentListActivity.this.initData();
			}
		});
	}

	private void addData() {
		NetUtil.get(this, new StringBuilder(Constant.COMMENT).append(this.id).append("?page=").append(this.page)
				.toString(), 100, new RequestCallBack<String>() {
			@Override
			public void onSuccess(ResponseInfo<String> responseInfo) {
				CommentListActivity.this.dialog.dismiss();
				try {
					JSONObject jsonObject = new JSONObject(responseInfo.result);
					if (jsonObject.getInt("code") == 10000) {
						JSONArray jsonArray = jsonObject.optJSONObject("data").getJSONArray("results");
						List<Comment> listComments = new ArrayList();
						for (int i = 0; i < jsonArray.length(); i++) {
							try {
								listComments.add(new Comment().parseJson(jsonArray.getJSONObject(i)));
							} catch (JSONException e) {
								e.printStackTrace();
							}
						}
						if (listComments == null || listComments.size() <= 0) {
							CommentListActivity.this.lv_comment.hideFooterView(false);
							MainToast.show(CommentListActivity.this, "\u6ca1\u6709\u66f4\u591a\u6570\u636e\u4e86", 0);
							return;
						}
						CommentListActivity.this.list.addAll(listComments);
						CommentListActivity.this.lv_comment.hideFooterView(true);
						CommentListActivity.this.initAdapter();
						return;
					}
					MainToast.show(CommentListActivity.this, jsonObject.getString("error"), 0);
				} catch (JSONException e2) {
					e2.printStackTrace();
				}
			}

			@Override
			public void onFailure(HttpException arg0, String arg1) {
				CommentListActivity.this.lv_comment.hideFooterView(true);
				CommentListActivity.this.dialog.dismiss();
				MainToast.show(CommentListActivity.this,
						CommentListActivity.this.getResources().getString(R.string.netFail), 0);
			}
		});
	}

	private void initAdapter() {
		this.commentAdapter.setList(this.list);
		this.commentAdapter.notifyDataSetChanged();
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_back /* 2131165198 */:
			finish();
		default:
		}
	}
}
